#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Name:       CamProxy
Version:    0.1.2
Purpose:    This programm takes several RTSP Motion JPEG streams and broadcasts them over HTTP.
            Optionally can reduce frame rate and picture size
Author:     Sergey Lalov
Date:       2011-02-18
License:    GPL
Usage:      All parameters are set up in config.py file
            Access URL by default is:
            http://hostname:8080/
            
            Run by:
            python camproxy.py
Target:     Cross Platform
Require:    Python 2.6. Modules: zope.interface, twisted, Image (python-imaging)
"""

from twisted.internet import reactor

from config import config
import rtsp_client
import rtp_mjpeg_client
import camproxy_process
import mjpeg_server

def main():
    print "CamProxy 0.1.2 - Motion JPEG over HTTP proxy for RTSP MJPEG sources"
    Process = camproxy_process.Process()
    conf = config()
    
    # Network cameras mjpeg grabbers
    i = 0
    for index, camera in enumerate(conf.cameras):
        reactor.listenUDP(41760 + i, rtp_mjpeg_client.RTP_MJPEG_Client(Process, index))
        reactor.connectTCP(camera['ip'], camera['port'], rtsp_client.RTSPFactory(index))
        i += 2

    # mjpeg http server
    reactor.listenTCP(conf.http_port, mjpeg_server.MJPEG_Factory(Process))
    reactor.run()
    
    print "Reactor stopped"

# this only runs if the module was *not* imported
if __name__ == '__main__':
    main()
